package foundation;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Date;

import static java.util.Arrays.asList;

/**
 * <h1></h1>
 *
 * @author zhh 2019-05-30
 */
public class GsonExample {
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("jean");
        student.setAge(16);
        student.setBirthday("2010-03-01");
        student.setHobby(new String[]{"sing", "dance"});
        student.setIgnore("ignore");
        student.setCreateTime(new Date(System.currentTimeMillis()));
        student.setScore(asList(85, 90, 100));
        Gson gson = new Gson();
        System.out.println(gson.toJson(student));
        System.out.println("----------");
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson1 = gsonBuilder.create();
        System.out.println(gson1.toJson(student));
        System.out.println("----------");
        gsonBuilder.setFieldNamingStrategy(field -> {
            if ("name".equals(field.getName())){
                return "NAME";
            }
            return field.getName();
        });
        Gson gson2 = gsonBuilder.create();
        System.out.println(gson2.toJson(student));
    }
}
